<template>
  <!-- 公司设置 -->
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 角色管理 -->
          <el-tab-pane label="角色管理">
            <el-row style="height: 60px">
              <el-button
                size="small"
                type="primary"
                icon="el-icon-plus"
                @click="dialogVisible=true"
              >新增角色</el-button>
            </el-row>
            <el-table border style="width: 100%" :data="list">
              <el-table-column type="index" label="序号" width="120">
              </el-table-column>
              <el-table-column prop="name" label="名称" width="240">
              </el-table-column>
              <el-table-column prop="description" label="描述">
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="{ row }">
                  <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="editEmployee(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delEmployee(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                layout="prev, pager, next"
                :current-page="page.page"
                :page-size="page.pagesize"
                :total="page.total"
                @current-change="changepage"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            >
            </el-alert>
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                ></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 对话框 -->
      <!-- 编辑角色 -->
      <el-dialog
        :title="roleForm.id ? '编辑角色' : '新增角色'"
        :visible.sync="dialogVisible"
        width="50%"
        @close="btnno"
      >
        <el-form
          ref="roleForm"
          :model="roleForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <el-row
          slot="footer"
          class="dialog-footer"
          type="flex"
          justify="center"
        >
          <el-button @click="btnno">取 消</el-button>
          <el-button type="primary" @click="btnok">确 定</el-button>
        </el-row>
      </el-dialog>
      <!-- 分配权限对话框 -->
      <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
        <!-- 权限是一颗树 -->
        <!-- 将数据绑定到组件上 -->
        <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
        <!-- id作为唯一标识 -->
        <el-tree
          ref="permTree"
          :data="permData"
          :props="defaultProps"
          :show-checkbox="true"
          :check-strictly="true"
          :default-expand-all="true"
          :default-checked-keys="selectCheck"
          node-key="id"
        />
        <!-- 确定 取消 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
            <el-button size="small" @click="btnPermCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoles, getCompanyInfo, delEmployee, editEmployee, getEmployee, addEmployee, assignPerm } from '@/api/setting'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  name: 'Setting',
  components: {},
  props: {},
  data() {
    return {
      page: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      list: [], // 角色数据
      companyInfo: {}, // 公司信息
      roleForm: { name: '', description: '' }, // 编辑角色表单数据
      rules: { name: [{ required: true, message: '请输入用户角色', trigger: 'blur' }] },
      dialogVisible: false, // 编辑角色对话框状态
      showPermDialog: false, // 权限分配对话框状态
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },
  async created() {
    this.getRoles()
    this.companyInfo = await getCompanyInfo(this.$store.state.user.userinfo.companyId)
  },
  mounted() {},
  methods: {
    // 获取角色数据
    async getRoles() {
      const { total, rows } = await getRoles(this.page)
      if (rows.length === 0 && this.page.page > 1) {
        this.page.page--
        this.getRoles()
      }
      this.list = rows
      this.page.total = total
    },
    // 下一页
    changepage(newPage) {
      // 修改数据
      this.page.page = newPage
      // 重新获取页面数据
      this.getRoles()
    },
    // 删除员工信息
    delEmployee(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delEmployee(id)
        this.$message.success('删除成功')
        this.getRoles()// 重新获取数据
      }).catch(() => {

      })
    },
    // 编辑按钮
    async editEmployee(id) {
      this.roleForm = await getEmployee(id)
      this.dialogVisible = true
    },
    // 编辑确定
    async btnok() {
      try {
        // 表单验证
        await this.$refs.roleForm.validate()
        if (this.roleForm.id) {
          await editEmployee(this.roleForm)
        } else {
          await addEmployee(this.roleForm)
        }
        // 调用编辑接口
        this.$message.success('操作成功')
        this.getRoles()
        this.dialogVisible = false
      } catch (error) {
        console.log(error)
      }
    },
    // 编辑取消
    btnno() {
      this.roleForm = this.$options.data().roleForm
      this.$refs.roleForm.resetFields()
      this.dialogVisible = false
    },
    // 点击分配权限
    // 获取权限点数据 在点击的时候调用 获取权限点数据
    async assignPerm(id) {
      this.showPermDialog = true
      this.permData = tranListToTreeData(await getPermissionList(), '0') // 转化list到树形数据
      this.roleId = id
      // 应该去获取 这个id的 权限点
      // 有id 就可以 id应该先记录下来
      const { permIds } = await getEmployee(id) // permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds // 将当前角色所拥有的权限id赋值
    },
    // 权限分配确定
    async  btnPermOK() {
      // 调用el-tree的方法
      // console.log(this.$refs.permTree.getCheckedKeys())
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    },
    // 权限分配取消
    btnPermCancel() {
      this.selectCheck = [] // 重置数据
      this.showPermDialog = false
    }

  }
}
</script>

<style lang="scss" scoped>
</style>
